package com.ecarx.module_vehicle.signal.adatpter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.library_base.BaseApplication;
import com.ecarx.module_vehicle.R;
import com.ecarx.module_vehicle.signal.Model.AreaId;
import com.ecarx.module_vehicle.signal.Model.InPropertyValue;
import com.ecarx.module_vehicle.signal.Model.Rate;
import com.ecarx.module_vehicle.signal.Model.SignalItem;
import com.ecarx.module_vehicle.signal.Model.UISelectPosition;
import com.ecarx.module_vehicle.signal.Model.WrappedPropertyId;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SignalAdapter extends RecyclerView.Adapter<SignalAdapter.MyViewHolder> {
    private final List<SignalItem> mDataList = new ArrayList<>();
    public static final String TAG = "SignalAdapter";
    private final Context mContext;
    private final OnSignalAdapterCallback onSignalAdapterCallback;
    private String mModuleName;

    public SignalAdapter(Context mContext, OnSignalAdapterCallback onSignalAdapterCallback) {
        this.mContext = mContext;
        this.onSignalAdapterCallback = onSignalAdapterCallback;
    }

    public void setList(List<SignalItem> datas, String moduleName) {
        mModuleName = moduleName;
        mDataList.clear();
        mDataList.addAll(datas);
        notifyDataSetChanged();
    }

    public String getModuleName() {
        return mModuleName;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_detail_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        SignalItem bean = mDataList.get(position);
        initSpinner(holder, bean);
        holder.tvName.setText(bean.getWrappedPropertyId().getShortName());
        boolean registered = bean.getUiSelectPosition().isRegistered();
        holder.cbItem.setOnCheckedChangeListener(null);
        holder.cbItem.setChecked(registered);
        holder.cbItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                UISelectPosition uiSelectPosition = mDataList.get(position).getUiSelectPosition();
                if (uiSelectPosition.isRegistered() != isChecked) {
                    uiSelectPosition.setRegistered(isChecked);
                    if (isChecked) {
                        Rate rate = bean.getRateList().get(holder.spRate.getSelectedItemPosition());
                        onSignalAdapterCallback.onRegisterCallback(bean.getModuleName(), bean.getWrappedPropertyId(), rate);
                    } else {
                        onSignalAdapterCallback.onUnRegisterCallback(bean.getModuleName(), bean.getWrappedPropertyId());
                    }
                }
            }
        });
        if (bean.getWrappedPropertyId().isCanSet()) {
            holder.btnSet.setOnClickListener(v -> {
                AreaId areaId = bean.getAreaIdList().get(holder.spArea.getSelectedItemPosition());
                InPropertyValue inPropertyValue = bean.getInPropertyValues().get(holder.spInPropValue.getSelectedItemPosition());
                onSignalAdapterCallback.onSetCallback(bean.getModuleName(), bean.getWrappedPropertyId(), areaId, inPropertyValue);
            });
            holder.btnSet.setEnabled(true);
        } else {
            holder.btnSet.setEnabled(false);
        }

        if (bean.getWrappedPropertyId().isCanGet()) {
            holder.btnGet.setOnClickListener(v -> {
                AreaId areaId = bean.getAreaIdList().get(holder.spArea.getSelectedItemPosition());
                onSignalAdapterCallback.onGetCallback(bean.getModuleName(), bean.getWrappedPropertyId(), areaId);
            });
            holder.btnGet.setEnabled(true);
        } else {
            holder.btnGet.setEnabled(false);
        }
    }

    private void initSpinner(MyViewHolder holder, SignalItem bean) {
        UISelectPosition uiSelectPosition = bean.getUiSelectPosition();
        holder.inPropValueAdapter.clear();
        holder.aredIdAdapter.clear();
        holder.rateAdapter.clear();
        holder.inPropValueAdapter.addAll(bean.getInPropertyValues().stream().map(new Function<InPropertyValue, String>() {
            @Override
            public String apply(InPropertyValue value) {
                return value.getShortName();
            }
        }).collect(Collectors.toList()));
        holder.rateAdapter.addAll(bean.getRateList().stream().map(new Function<Rate, String>() {
            @Override
            public String apply(Rate value) {
                return value.getName();
            }
        }).collect(Collectors.toList()));
        holder.aredIdAdapter.addAll(bean.getAreaIdList().stream().map(new Function<AreaId, String>() {
            @Override
            public String apply(AreaId value) {
                return value.getShortName();
            }
        }).collect(Collectors.toList()));
        holder.spArea.setSelection(uiSelectPosition.getAreaIdSelectPosition());
        holder.spInPropValue.setSelection(uiSelectPosition.getInPropertyValueSelectPosition());
        holder.spRate.setSelection(uiSelectPosition.getRateSelectPosition());
        boolean canGet = bean.getWrappedPropertyId().isCanGet();
        if (canGet) {
            holder.spArea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    uiSelectPosition.setAreaIdSelectPosition(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
        }
        boolean canSet = bean.getWrappedPropertyId().isCanSet();
        holder.spArea.setEnabled(canGet || canSet);
        holder.spArea.setAlpha(canGet || canSet ? 1f : 0.5f);
        holder.spInPropValue.setEnabled(canSet);
        holder.spInPropValue.setAlpha(canSet ? 1f : 0.5f);
        if (canSet) {
            holder.spInPropValue.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    uiSelectPosition.setInPropertyValueSelectPosition(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
        }
        holder.spRate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                uiSelectPosition.setRateSelectPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvName, btnSet, btnGet;
        CheckBox cbItem;
        Spinner spArea;
        Spinner spRate;
        Spinner spInPropValue;
        ArrayAdapter<String> inPropValueAdapter;
        ArrayAdapter<String> aredIdAdapter;
        ArrayAdapter<String> rateAdapter;

        public MyViewHolder(View itemView) {
            super(itemView);
            cbItem = itemView.findViewById(R.id.cb_item);
            tvName = itemView.findViewById(R.id.tv_name);
            spArea = itemView.findViewById(R.id.spinner_location);
            spRate = itemView.findViewById(R.id.spinner_frequency);
            spInPropValue = itemView.findViewById(R.id.spinner_set_value);
            btnSet = itemView.findViewById(R.id.btn_set);
            btnGet = itemView.findViewById(R.id.btn_get);
            inPropValueAdapter = getSpinnerAdapter();
            aredIdAdapter = getSpinnerAdapter();
            rateAdapter = getSpinnerAdapter();
            spRate.setAdapter(rateAdapter);
            spInPropValue.setAdapter(inPropValueAdapter);
            spArea.setAdapter(aredIdAdapter);
        }

        private ArrayAdapter<String> getSpinnerAdapter() {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(BaseApplication.getInstance(), R.layout.item_spineer_text);
            adapter.setDropDownViewResource(R.layout.item_spineer_text);
            return adapter;
        }
    }

    public interface OnSignalAdapterCallback {
        void onRegisterCallback(String module, WrappedPropertyId wrappedPropertyId, Rate rate);

        void onUnRegisterCallback(String module, WrappedPropertyId wrappedPropertyId);

        void onSetCallback(String module, WrappedPropertyId wrappedPropertyId, AreaId areaId, InPropertyValue inPropertyValue);

        void onGetCallback(String module, WrappedPropertyId wrappedPropertyId, AreaId areaId);
    }
}
